﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Project_ScansiaPacific
{
    public partial class FrTKNLVaoRa : Form
    {
        public FrTKNLVaoRa()
        {
            InitializeComponent();
        }
        private string Nguon1,Dich1,MaSP_Nguon1, MaSP_Dich1, TenSP_Nguon1,MaCT_Nguon1,TenCT_Nguon1, TenSP_Dich1,TenCT_Dich1,MaCT_Dich1, CDay_Nguon1, CRong_Nguon1, CDai_Nguon1, CDay_Dich1, CRong_Dich1, CDai_Dich1, SoLuong_Nguon1, SoLuong_Dich1, NguonGo_Nguon1, NguonGo_Dich1, PhanLoai_Nguon1, PhanLoai_Dich1, KTG_Dich1, TanDung_Dich1,Tramtret_Dich1, Ghep_Dich1, Bo_Dich1, Khac_Dich1,child=" - ",Ngay_Nguon1,NguoiGhiChep1,NhaCungCap1,maxID,MBao_Dich1,MCat_Dich1,MRong_Dich1,Ngay_Dich1,datebegin1,dateend1;
        private double SoLuong_Nguon11 = 0, SoLuong_Dich11, KTG_Dich11, TramTret_Dich11, TanDung_Dich11, Ghep_Dich11, Bo_Dich11, Khac_Dich11,SoKhoi_Nguon11,CDai_Nguon11,CRong_Nguon11,CDay_Nguon11,CDay_Dich11,CRong_Dich11,CDai_Dich11,MBao_Dich11,MRong_Dich11,MCat_Dich11,SoKhoi_Dich11=0;
        private void combo_load()
        {
            Data dta = new Data();
            FormProcess Frp = new FormProcess();
            Process prs = new Process();
            string sql_load = @"select * from dbo.Load_cbSP";
            Frp.cb1(cb_TenSP_Nguon, sql_load, 1, 0);
            Frp.cb1(cb_TenSP_Dich, sql_load, 1, 0);
            sql_load = @"select MaNCC,TenNCC from dbo.NhaCungCap";
            Frp.cb1(cb_NhaCC, sql_load, 1, 0);
            sql_load = @"select MaChiTiet,ChieuDaySC,ChieuRongSC,ChieuDaiSC from dbo.ChiTietSP inner join dbo.Load_cbSP on ChiTietSP.MaSP=Load_cbSP.MaSP";
            DataSet ds = prs.dts(sql_load);
            dataGridViewX2.DataSource = ds.Tables[0];
            //MessageBox.Show(dataGridViewX2.Rows. Count.ToString());
            string[] arr_Nguon = new string[] {"Kho Gỗ - KGo", "Máy Rong - MRo", "Máy Cắt KT - MCKTa", "Máy Cắt Hơi - MCHo", "Lựa - Lu","Trám trét - TTr","Máy Bào - MBa","Ghép - Gh","Tận Dụng - TDu","Kho TG - KTGi","Khác - Kh","Phân Loại - PLo","CĐK - CĐK" };
            for (int j = 0; j < arr_Nguon.Length; j++)
            {
                cb_Nguon.Items.Add(arr_Nguon[j].ToString());
                cb_Dich.Items.Add(arr_Nguon[j].ToString());
                cb_NoiGiao_S.Items.Add(arr_Nguon[j].ToString());
                cb_NoiNhan_S.Items.Add(arr_Nguon[j].ToString());
               
            }

            string[] arr_PL = new string[] { "2AB", "2AC", "3A", "3AB", "3AC", "3BB", "3BC", "CC","Khác","3AC(CH)","3AC(KT)" };
            for (int k = 0; k < arr_PL.Length; k++)
            {
                cb_PhanLoai_Nguon.Items.Add(arr_PL[k].ToString());
                cb_PhanLoai_Dich.Items.Add(arr_PL[k].ToString());
                // cb_LyDoXuat.Items.Add(arr_LyDo[j].ToString());
            }
            string[] arr_NguonGo = new string[] { "ACACIA", "EU", "Khác" };
            for (int m = 0; m < arr_NguonGo.Length; m++)
            {
                cb_NguonGo_Nguon.Items.Add(arr_NguonGo[m].ToString());
                cb_NguonGo_Dich.Items.Add(arr_NguonGo[m].ToString());
                // cb_LyDoXuat.Items.Add(arr_LyDo[j].ToString());
            }
            
        }

        private void FrTKNLVaoRa_Load(object sender, EventArgs e)
        {
            combo_load();
            //Enable_ChiTietHC();
            Enable_Begin();
            cb_Nguon.Focus();
        }
        private void Enable_Begin()
        {
            cb_Nguon.Focus();
            cb_Nguon.Enabled = true;
            cb_Dich.Enabled = true;
            cb_TenSP_Nguon.Enabled = false;
            cb_TenCTNguon.Enabled = false;
            cb_TenSP_Dich.Enabled = false;
            cb_TenCTDich.Enabled = false;
            txt_KTG_Dich.Enabled = false;
            txt_Tramtret_Dich.Enabled = false;
            txt_TanDung_Dich.Enabled = false;
            txt_Ghep_Dich.Enabled = false;
            txt_Bo_Dich.Enabled = false;
            txt_Khac_Dich.Enabled = false;
            txt_Day_Nguon.Enabled = false;
            txt_Day_Dich.Enabled = false;
            txt_Rong_Nguon.Enabled = false;
            txt_Rong_Dich.Enabled = false;
            txt_Dai_Nguon.Enabled = false;
            txt_Dai_Dich.Enabled = false;
            txt_SoLuong_Nguon.Enabled = false;
            txt_SL_Dich.Enabled = false;
            cb_PhanLoai_Nguon.Enabled = false;
            cb_PhanLoai_Dich.Enabled = false;
            txt_MRong_Dich.Enabled = false;
            txt_Bao_Dich.Enabled = false;
            txt_Cat_Dich.Enabled = false;
        }
        private void Enable_Phach()
        {
            cb_TenSP_Nguon.Enabled = false;
            cb_TenCTNguon.Enabled = false;
            cb_TenSP_Dich.Enabled = false;
            cb_TenCTDich.Enabled = false;
            txt_KTG_Dich.Enabled = false;
            txt_Tramtret_Dich.Enabled = false;
            txt_TanDung_Dich.Enabled = false;
            txt_Ghep_Dich.Enabled = false;
            txt_Bo_Dich.Enabled = false;
            txt_Khac_Dich.Enabled = false;
            txt_Day_Nguon.Enabled = true;
            txt_Day_Dich.Enabled = true;
            txt_Rong_Nguon.Enabled = true;
            txt_Rong_Dich.Enabled = true;
            txt_Dai_Nguon.Enabled = true;
            txt_Dai_Dich.Enabled = true;
            txt_SoLuong_Nguon.Enabled = true;
            txt_SL_Dich.Enabled = true;
            cb_PhanLoai_Nguon.Enabled = true;
            cb_PhanLoai_Dich.Enabled = true;
            txt_MRong_Dich.Enabled = false;
            txt_Bao_Dich.Enabled = false;
            txt_Cat_Dich.Enabled = false;

        }
        private void Enable_SoChe()
        {
            cb_TenSP_Nguon.Enabled = false;
            cb_TenCTNguon.Enabled = false;
            cb_TenSP_Dich.Enabled = true;
            cb_TenCTDich.Enabled = true;
            txt_KTG_Dich.Enabled = false;
            txt_Tramtret_Dich.Enabled = false;
            txt_TanDung_Dich.Enabled = false;
            txt_Ghep_Dich.Enabled = false;
            txt_Bo_Dich.Enabled = false;
            txt_Khac_Dich.Enabled = false;
            txt_Day_Nguon.Enabled = true;
            txt_Day_Dich.Enabled = false;
            txt_Rong_Nguon.Enabled = true;
            txt_Rong_Dich.Enabled = false;
            txt_Dai_Nguon.Enabled = true;
            txt_Dai_Dich.Enabled = false;
            txt_SoLuong_Nguon.Enabled = true;
            txt_SL_Dich.Enabled = true;
            cb_PhanLoai_Nguon.Enabled = true;
            cb_PhanLoai_Dich.Enabled = false;
            txt_MRong_Dich.Enabled = false;
            txt_Bao_Dich.Enabled = false;
            txt_Cat_Dich.Enabled = false;
        }
        private void Enable_ChiTiet()
        {
            cb_TenSP_Nguon.Enabled = true;
            cb_TenCTNguon.Enabled = true;
            cb_TenSP_Dich.Enabled = true;
            cb_TenCTDich.Enabled = true;
            txt_KTG_Dich.Enabled = false;
            txt_Tramtret_Dich.Enabled = false;
            txt_TanDung_Dich.Enabled = false;
            txt_Ghep_Dich.Enabled = false;
            txt_Bo_Dich.Enabled = false;
            txt_Khac_Dich.Enabled = false;
            txt_Day_Nguon.Enabled = false;
            txt_Day_Dich.Enabled = false;
            txt_Rong_Nguon.Enabled = false;
            txt_Rong_Dich.Enabled = false;
            txt_Dai_Nguon.Enabled = false;
            txt_Dai_Dich.Enabled = false;
            txt_SoLuong_Nguon.Enabled = true;
            txt_SL_Dich.Enabled = true;
            cb_PhanLoai_Nguon.Enabled = false;
            cb_PhanLoai_Dich.Enabled = false;
            txt_MRong_Dich.Enabled = false;
            txt_Bao_Dich.Enabled = false;
            txt_Cat_Dich.Enabled = false;

        }
        private void Enable_ChiTietHC()
        {
            cb_TenSP_Nguon.Enabled = true;
            cb_TenCTNguon.Enabled = true;
            cb_TenSP_Dich.Enabled = true;
            cb_TenCTDich.Enabled = true;
            txt_KTG_Dich.Enabled = true;
            txt_Tramtret_Dich.Enabled = true;
            txt_TanDung_Dich.Enabled = true;
            txt_Ghep_Dich.Enabled = true;
            txt_Bo_Dich.Enabled = true;
            txt_Khac_Dich.Enabled = true;
            txt_Day_Nguon.Enabled = false;
            txt_Day_Dich.Enabled = false;
            txt_Rong_Nguon.Enabled = false;
            txt_Rong_Dich.Enabled = false;
            txt_Dai_Nguon.Enabled = false;
            txt_Dai_Dich.Enabled = false;
            txt_SoLuong_Nguon.Enabled = true;
            txt_SL_Dich.Enabled = false;
            cb_PhanLoai_Nguon.Enabled = false;
            cb_PhanLoai_Dich.Enabled = false;
            txt_MRong_Dich.Enabled = true;
            txt_Bao_Dich.Enabled = true;
            txt_Cat_Dich.Enabled = true;

        }
        private void Enable_select()
        {
            Process prs = new Process();
            try
            {
                if (cb_Nguon.Text != "" && cb_Dich.Text != "")
                {
                    Nguon1 = prs.substring_right(cb_Nguon.Text, child);
                    Dich1 = prs.substring_right(cb_Dich.Text, child);
                    if (Nguon1 == "" || Dich1 == "")
                        MessageBox.Show("Nhập sai thông tin");
                    else
                    {
                        if (Nguon1 == "KGo")
                            Enable_Phach();
                        if (Nguon1 == "MRo")
                            Enable_SoChe();
                        if (Nguon1 == "MCKTa" || Nguon1 == "MCHo")
                        {
                            if (Dich1 == "MRo")
                                Enable_ChiTiet();
                            if (Dich1 == "MBa")
                                Enable_ChiTietHC();
                        }
                        if (Nguon1 == "MBa")
                        {
                            if (Dich1 == "TTr")
                                Enable_ChiTietHC();
                            if (Dich1 == "Gh")
                                Enable_ChiTiet();

                        }
                        if (Nguon1 == "PLo")
                        {
                            if (Dich1 == "MCKTa" || Dich1 == "MCHo")
                                Enable_SoChe();

                        }
                        if (Nguon1 == "TDu")
                        {
                            Enable_ChiTietHC();
                        }
                        if (Nguon1 == "TTr")
                        {
                            if (Dich1 == "TDu")
                                Enable_ChiTiet();
                        }
                        if (Nguon1 == "Gh")
                        {
                            if (Dich1 == "MBa")
                                Enable_ChiTietHC();
                        }
                       

                    }
                }
                else
                    MessageBox.Show("Bạn chưa nhập Nguồn và đích");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cb_Dich_Leave(object sender, EventArgs e)
        {
            Enable_select();
            cb_TenSP_Nguon.Text = "";
            cb_TenCTNguon.Text = "";
            cb_TenCTDich.Text = "";
            cb_TenSP_Dich.Text = "";
        }
        private void input_value()
        {
            if (txt_Day_Nguon.Text == "")
                CDay_Nguon1 = "0";
            else
            {
                CDay_Nguon1 = txt_Day_Nguon.Text;
                
            }
            if (txt_Day_Dich.Text == "")
                CDay_Dich1 = "0";
            else
            {
                CDay_Dich1 = txt_Day_Dich.Text;
                CDay_Dich11 = double.Parse(CDay_Dich1);
            }
            if (txt_Rong_Nguon.Text == "")
                CRong_Nguon1 = "0";
            else
                CRong_Nguon1 = txt_Rong_Nguon.Text;
            if (txt_Rong_Dich.Text == "")
                CRong_Dich1 = "0";
            else
            {
                CRong_Dich1 = txt_Rong_Dich.Text;
                CRong_Dich11 = double.Parse(CRong_Dich1);
            }
            if (txt_Dai_Nguon.Text == "")
                CDai_Nguon1 = "0";
            else
                CDai_Nguon1 = txt_Dai_Nguon.Text;
            if (txt_Dai_Dich.Text == "")
                CDai_Dich1 = "0";
            else
            {
                CDai_Dich1 = txt_Dai_Dich.Text;
                CDai_Dich11 = double.Parse(CDai_Dich1);
            }
            if (txt_SoLuong_Nguon.Text == "")
                SoLuong_Nguon1 = "0";
            else
                SoLuong_Nguon1 = txt_SoLuong_Nguon.Text;
            if (txt_SL_Dich.Text == "")
                SoLuong_Dich1 = "0";
            else
                SoLuong_Dich1 = txt_SL_Dich.Text;
            if (txt_KTG_Dich.Text == "")
                KTG_Dich1 = "0";
            else
                KTG_Dich1 = txt_KTG_Dich.Text;
            if (txt_Tramtret_Dich.Text == "")
                Tramtret_Dich1 = "0";
            else
                Tramtret_Dich1 = txt_Tramtret_Dich.Text;
            if (txt_TanDung_Dich.Text == "")
                TanDung_Dich1 = "0";
            else
                TanDung_Dich1 = txt_TanDung_Dich.Text;
            if (txt_Ghep_Dich.Text == "")
                Ghep_Dich1 = "0";
            else
                Ghep_Dich1 = txt_Ghep_Dich.Text;
            if (txt_Bo_Dich.Text == "")
                Bo_Dich1 = "0";
            else
                Bo_Dich1 = txt_Bo_Dich.Text;
            if (txt_Khac_Dich.Text == "")
                Khac_Dich1 = "0";
            else
                Khac_Dich1 = txt_Khac_Dich.Text;
            if (txt_Bao_Dich.Text == "")
                MBao_Dich1 = "0";
            else
                MBao_Dich1 = txt_Bao_Dich.Text;
            if (txt_Cat_Dich.Text == "")
                MCat_Dich1 = "0";
            else
                MCat_Dich1 = txt_Cat_Dich.Text;
            if (txt_MRong_Dich.Text == "")
                MRong_Dich1 = "0";
            else
                MRong_Dich1 = txt_MRong_Dich.Text;

            PhanLoai_Nguon1 = cb_PhanLoai_Nguon.Text;
            PhanLoai_Dich1 = cb_PhanLoai_Dich.Text;
            NguonGo_Nguon1 = cb_NguonGo_Nguon.Text;
            NguonGo_Dich1 = cb_NguonGo_Dich.Text;
        }
        private void insert_grid()
        {
            Process prs = new Process();
            try
            {
                cb_Dich.Enabled = false;
                cb_Nguon.Enabled = false;
                //MessageBox.Show("aaaa");
                input_value();
                CDay_Nguon11 = double.Parse(txt_Day_Nguon.Text);
                CRong_Nguon11 = double.Parse(txt_Rong_Nguon.Text);
                CDai_Nguon11 = double.Parse(txt_Dai_Nguon.Text);
                SoLuong_Nguon11 = double.Parse(txt_SoLuong_Nguon.Text);
                SoKhoi_Nguon11 = CDay_Nguon11 * CRong_Nguon11 * CDai_Nguon11 * SoLuong_Nguon11/1000000000;
                lb_SoKhoiVao.Text = string.Format("{0:###,###.####}", SoKhoi_Nguon11);
                CDay_Dich11 = double.Parse(txt_Day_Dich.Text);
                CRong_Dich11 = double.Parse(txt_Rong_Dich.Text);
                CDai_Dich11 = double.Parse(txt_Dai_Dich.Text);
                //MessageBox.Show("bbb");
                KTG_Dich11 = double.Parse(KTG_Dich1);
                TramTret_Dich11 = double.Parse(Tramtret_Dich1);
                //MessageBox.Show("ccc");
                TanDung_Dich11 = double.Parse(TanDung_Dich1);
                Ghep_Dich11 = double.Parse(Ghep_Dich1);
                MBao_Dich11 = double.Parse(MBao_Dich1);
                MRong_Dich11 = double.Parse(MRong_Dich1);
               // MessageBox.Show("ddd");
                MCat_Dich11 = double.Parse(MCat_Dich1);
                Bo_Dich11 = double.Parse(Bo_Dich1);
                Khac_Dich11 = double.Parse(Khac_Dich1);
                SoLuong_Dich11 = double.Parse(SoLuong_Dich1);
                SoKhoi_Dich11 = CDay_Dich11 * CRong_Dich11 * CDai_Dich11 * (SoLuong_Dich11 + KTG_Dich11 + TramTret_Dich11 + TanDung_Dich11 + Ghep_Dich11 + MBao_Dich11 + MRong_Dich11 + MCat_Dich11 + Bo_Dich11 + Khac_Dich11) / 1000000000;
                //MessageBox.Show(SoKhoi_Dich11.ToString());
                if (cb_TenSP_Dich.Enabled == true && cb_TenCTDich.Enabled == true)
                {
                    if (cb_TenSP_Dich.Text == "" || cb_TenCTDich.Text == "")
                    {
                        MessageBox.Show("Nhập thiếu thông tin");
                    }
                    else
                    {
                        TenSP_Dich1 = prs.substring_left(cb_TenSP_Dich.Text, child);
                        MaSP_Dich1 = prs.substring_right(cb_TenSP_Dich.Text, child);
                        TenCT_Dich1 = prs.substring_left(cb_TenCTDich.Text, child);
                        MaCT_Dich1 = prs.substring_right(cb_TenCTDich.Text, child);

                        if (TenCT_Dich1 == "" || TenSP_Dich1 == "" || MaCT_Dich1 == "" || MaSP_Dich1 == "")
                            MessageBox.Show("Nhập sai thông tin");
                        else
                        {
                            dataGridViewX1.Rows.Add(TenSP_Dich1, TenCT_Dich1, CDay_Dich11, CRong_Dich11, CDai_Dich11, SoLuong_Dich1,NguonGo_Dich1,PhanLoai_Dich1, KTG_Dich1, Tramtret_Dich1, TanDung_Dich1, Ghep_Dich1,MBao_Dich1,MRong_Dich1,MCat_Dich1, Bo_Dich1, Khac_Dich1, MaSP_Dich1, MaCT_Dich1);
                            SoKhoi_Dich11 += double.Parse(lb_SoKhoiRa.Text);
                            
                            prs.formatcolordatagrid(dataGridViewX1); 
                            groupBox1.Enabled=false;
                            cb_TenCTDich.Text = "";
                            txt_Rong_Dich.Text = "";
                            txt_SL_Dich.Text = "";
                            txt_KTG_Dich.Text = "";
                            txt_Tramtret_Dich.Text = "";
                            txt_TanDung_Dich.Text = "";
                            txt_Ghep_Dich.Text = "";
                            txt_Bo_Dich.Text = "";
                            txt_Khac_Dich.Text = "";
                            txt_Cat_Dich.Text = "";
                            txt_MRong_Dich.Text = "";
                            txt_Bao_Dich.Text = "";
                            //prs.reset_group_txt(groupBox2);
                            cb_TenSP_Dich.Focus();
                            if (SoKhoi_Dich11 > SoKhoi_Nguon11)
                            {
                                DialogResult d = MessageBox.Show("Số khối đầu ra đã LỚN HƠN số khối đầu vào, \n Nhấn YES nếu bạn đồng ý thêm, Nhấn NO để hủy bỏ", "Thông Báo", MessageBoxButtons.YesNo);
                                if (d == DialogResult.No)
                                {
                                    int i = dataGridViewX1.Rows.Count - 2;
                                    dataGridViewX1.Rows.RemoveAt(i);
                                }
                                if (d == DialogResult.Yes)
                                {
                                    lb_SoKhoiRa.Text = string.Format("{0:###,###.####}", SoKhoi_Dich11);
                                }
                            }
                            else
                                lb_SoKhoiRa.Text = string.Format("{0:###,###.####}", SoKhoi_Dich11);
                        }

                    }
                }
                if (cb_TenSP_Dich.Enabled == false && cb_TenCTDich.Enabled == false)
                {

                    dataGridViewX1.Rows.Add("","", CDay_Dich11, CRong_Dich11, CDai_Dich11, SoLuong_Dich1,NguonGo_Dich1,PhanLoai_Dich1, KTG_Dich1, Tramtret_Dich1, TanDung_Dich1, Ghep_Dich1,MBao_Dich1,MRong_Dich1,MCat_Dich1, Bo_Dich1, Khac_Dich1, "","");
                    prs.formatcolordatagrid(dataGridViewX1);
                    SoKhoi_Dich11 += double.Parse(lb_SoKhoiRa.Text);
                    txt_Rong_Dich.Text = "";
                    txt_SL_Dich.Text = "";
                    txt_KTG_Dich.Text = "";
                    txt_Tramtret_Dich.Text = "";
                    txt_TanDung_Dich.Text = "";
                    txt_Ghep_Dich.Text = "";
                    txt_MRong_Dich.Text = "";
                    txt_Bao_Dich.Text = "";
                    txt_Cat_Dich.Text = "";
                    txt_Bo_Dich.Text = "";
                    txt_Khac_Dich.Text = "";
                    groupBox1.Enabled=false;
                    //prs.reset_group_txt(groupBox2);
                    txt_Day_Dich.Focus();
                    if (SoKhoi_Dich11 > SoKhoi_Nguon11)
                    {
                        DialogResult d = MessageBox.Show("Số khối đầu ra đã LỚN HƠN số khối đầu vào, \n Nhấn YES nếu bạn đồng ý thêm, Nhấn NO để hủy bỏ", "Thông Báo", MessageBoxButtons.YesNo);
                        if (d == DialogResult.No)
                        {
                            int i = dataGridViewX1.Rows.Count - 2;
                            dataGridViewX1.Rows.RemoveAt(i);
                        }
                        if (d == DialogResult.Yes)
                        {
                            lb_SoKhoiRa.Text = string.Format("{0:###,###.####}", SoKhoi_Dich11);
                        }
                    }
                    else
                        lb_SoKhoiRa.Text = string.Format("{0:###,###.####}", SoKhoi_Dich11);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cb_TenSP_Nguon_SelectedIndexChanged(object sender, EventArgs e)
        {
            Process prs = new Process();
            FormProcess Frp = new FormProcess();
            MaSP_Nguon1 = prs.substring_right(cb_TenSP_Nguon.Text, child);
            cb_TenCTNguon.Items.Clear();
            cb_TenCTNguon.Text="";
            string sql_load = @"select MaChiTiet,TenChiTiet from dbo.ChiTietSP where MaSP='" + MaSP_Nguon1 + "'";
            Frp.cb1(cb_TenCTNguon, sql_load, 1, 0);
            cb_TenSP_Dich.Text = cb_TenSP_Nguon.Text;
        }

        private void cb_TenSP_Dich_SelectedIndexChanged(object sender, EventArgs e)
        {
            Process prs = new Process();
            FormProcess Frp = new FormProcess();
            MaSP_Dich1 = prs.substring_right(cb_TenSP_Dich.Text, child);
            cb_TenCTDich.Items.Clear();
            cb_TenCTDich.Text = "";
            string sql_load = @"select MaChiTiet,TenChiTiet from dbo.ChiTietSP where MaSP='" + MaSP_Dich1 + "'";
            Frp.cb1(cb_TenCTDich, sql_load, 1, 0);
        }

        private void cb_TenCTNguon_SelectedIndexChanged(object sender, EventArgs e)
        {
            Process prs=new Process();
            int i=0;
            string check = null;
            try
            {
                if(cb_TenCTNguon.Text!="")
                    MaCT_Nguon1=prs.substring_right(cb_TenCTNguon.Text,child);
                if (MaCT_Nguon1 == "")
                    MessageBox.Show("Nhập sai thông thông tin Chi Tiết");
                   
                else
                {
                    //MessageBox.Show(MaCT_Nguon1);
                    MaCT_Nguon1 = MaCT_Nguon1.Trim();
                    for (i = 0; i < dataGridViewX2.Rows.Count; i++)
                    {
                        check = dataGridViewX2.Rows[i].Cells["MaChiTiet"].Value.ToString().Trim();
                        if (MaCT_Nguon1 == check)
                        {
                            txt_Day_Nguon.Text = dataGridViewX2.Rows[i].Cells["ChieuDaySC"].Value.ToString();
                            txt_Rong_Nguon.Text = dataGridViewX2.Rows[i].Cells["ChieuRongSC"].Value.ToString();
                            txt_Dai_Nguon.Text = dataGridViewX2.Rows[i].Cells["ChieuDaiSC"].Value.ToString();
                            break;
                        }
                    }
                    if (i == dataGridViewX2.Rows.Count)
                    {
                        txt_Day_Nguon.Text = "0";
                        txt_Rong_Nguon.Text = "0";
                        txt_Dai_Nguon.Text = "0";
                        MessageBox.Show("Không có quy cách cho chi tiết này");
                    }
                    //MessageBox.Show(dataGridViewX2.Rows[85].Cells["MaChiTiet"].Value.ToString(), dataGridViewX2.Rows[86].Cells["MaChiTiet"].Value.ToString().Length.ToString());
                    cb_TenCTDich.Text = cb_TenCTNguon.Text; 
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txt_Day_Nguon_TextChanged(object sender, EventArgs e)
        {
            /*FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_Day_Nguon);
            txt_Day_Dich.Text = txt_Day_Nguon.Text;*/
        }

        private void txt_Rong_Nguon_TextChanged(object sender, EventArgs e)
        {
            /*FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_Rong_Nguon);*/
        }

        private void txt_Dai_Nguon_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void txt_SoLuong_Nguon_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_SoLuong_Nguon);
        }

        private void txt_Day_Dich_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void txt_Rong_Dich_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void txt_Dai_Dich_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void txt_SL_Dich_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_SL_Dich);
        }

        private void txt_KTG_Dich_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_KTG_Dich);
        }

        private void txt_Tramtret_Dich_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_Tramtret_Dich);
        }

        private void txt_TanDung_Dich_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_TanDung_Dich);
        }

        private void txt_Ghep_Dich_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_Ghep_Dich);
        }

        private void txt_Bo_Dich_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_Bo_Dich);
        }

        private void txt_Khac_Dich_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_Khac_Dich);
        }
        private bool check_Nguon()
        {
            int check = 0;
            Process prs = new Process();
            if (txt_SoLuong_Nguon.Text != "")
            {
                if (cb_TenCTNguon.Enabled == true)
                {
                    if (cb_TenCTNguon.Text != "")
                    {
                        TenCT_Nguon1 = prs.substring_left(cb_TenCTNguon.Text, child);
                        if (TenCT_Nguon1 == "")
                        {
                            MessageBox.Show("Nhập sai thông tin Chi Tiết");
                        }
                        else
                            check = 1;
                    }
                }
                else
                {
                    if (txt_Day_Nguon.Text == "" || txt_Rong_Nguon.Text == "" || txt_Dai_Nguon.Text == "")
                    {
                        MessageBox.Show("Nhập thiết thông tin");
                    }
                    else
                        check = 1;
                }
            }
            else
            {
                MessageBox.Show("Nhập thiếu Số lượng");
            }
            if (check == 0)
            return false;
            else
            return true;
            
        }
        private void bt_insert_Click(object sender, EventArgs e)
        {
            try
            {
                if (check_Nguon())
                    insert_grid();
                else
                    MessageBox.Show("Kiểm tra lại thông tin ở dữ liệu nguồn");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
   
        }

        private void cb_TenCTDich_SelectedIndexChanged(object sender, EventArgs e)
        {
            Process prs=new Process();
            int i=0;
            string check = null;
            try
            {
                if (cb_TenCTDich.Text != "")
                    MaCT_Nguon1 = prs.substring_right(cb_TenCTDich.Text, child);
                if (MaCT_Nguon1 == "")
                    MessageBox.Show("Nhập sai thông thông tin Chi Tiết");

                else
                {
                    //MessageBox.Show(MaCT_Nguon1);
                    MaCT_Nguon1 = MaCT_Nguon1.Trim();
                    for (i = 0; i < dataGridViewX2.Rows.Count; i++)
                    {
                        check = dataGridViewX2.Rows[i].Cells["MaChiTiet"].Value.ToString().Trim();
                        if (MaCT_Nguon1 == check)
                        {
                            txt_Day_Dich.Text = dataGridViewX2.Rows[i].Cells["ChieuDaySC"].Value.ToString();
                            txt_Rong_Dich.Text = dataGridViewX2.Rows[i].Cells["ChieuRongSC"].Value.ToString();
                            txt_Dai_Dich.Text = dataGridViewX2.Rows[i].Cells["ChieuDaiSC"].Value.ToString();
                            break;
                        }
                    }
                    if (i == dataGridViewX2.Rows.Count)
                    {
                        txt_Day_Dich.Text = "0";
                        txt_Rong_Dich.Text = "0";
                        txt_Dai_Dich.Text = "0";
                        //cb_TenCTDich.Text = cb_TenCTNguon.Text;
                        MessageBox.Show("Không có quy cách cho chi tiết này");
                    }
                    //MessageBox.Show(dataGridViewX2.Rows[85].Cells["MaChiTiet"].Value.ToString(), dataGridViewX2.Rows[86].Cells["MaChiTiet"].Value.ToString().Length.ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
                    
        }
        private void get_value_Nguon()
        {
            Process prs=new Process();
            Nguon1 = cb_Nguon.Text;
            Dich1 = cb_Dich.Text;
            string[] temp = txt_Ngay_Nguon.Text.Split('/');
            Ngay_Nguon1 = temp[1] + "/" + temp[0] + "/" + temp[2];
            NguoiGhiChep1 = txt_MaNV.Text;
            TenSP_Nguon1 = "";
            MaSP_Nguon1 = "";
            if (cb_TenSP_Nguon.Enabled == true)
            {
                TenSP_Nguon1 = prs.substring_left(cb_TenSP_Nguon.Text, child);
                MaSP_Nguon1 = prs.substring_right(cb_TenSP_Nguon.Text, child);
            }
            CDay_Nguon1 = txt_Day_Nguon.Text;
            CRong_Nguon1 = txt_Rong_Nguon.Text;
            CDai_Nguon1 = txt_Dai_Nguon.Text;
            SoLuong_Nguon1 = txt_SoLuong_Nguon.Text;
            NguonGo_Nguon1 = cb_NguonGo_Nguon.Text;
            PhanLoai_Nguon1 = cb_PhanLoai_Nguon.Text;
            NhaCungCap1 = cb_NhaCC.Text;


        }
        private void get_value_Dich(int i)
        {
            TenSP_Dich1=dataGridViewX1.Rows[i].Cells["TenSP_Dich"].Value.ToString();
            TenCT_Dich1 = dataGridViewX1.Rows[i].Cells["TenCT_Dich"].Value.ToString();
            CDay_Dich1 = dataGridViewX1.Rows[i].Cells["ChieuDay_Dich"].Value.ToString();
            CRong_Dich1 = dataGridViewX1.Rows[i].Cells["ChieuRong_Dich"].Value.ToString();
            CDai_Dich1 = dataGridViewX1.Rows[i].Cells["ChieuDai_Dich"].Value.ToString();
            SoLuong_Dich1 = dataGridViewX1.Rows[i].Cells["SoLuong"].Value.ToString();
            NguonGo_Dich1 = dataGridViewX1.Rows[i].Cells["NguonGo_Dich"].Value.ToString();
            PhanLoai_Dich1 = dataGridViewX1.Rows[i].Cells["PhanLoai_Dich"].Value.ToString();
            KTG_Dich1 = dataGridViewX1.Rows[i].Cells["KTG_Dich"].Value.ToString();
            Tramtret_Dich1 = dataGridViewX1.Rows[i].Cells["TramTret_Dich"].Value.ToString(); ;
            TanDung_Dich1 = dataGridViewX1.Rows[i].Cells["TanDung_Dich"].Value.ToString();
            Ghep_Dich1 = dataGridViewX1.Rows[i].Cells["Ghep_Dich"].Value.ToString();
            Bo_Dich1 = dataGridViewX1.Rows[i].Cells["Bo_Dich"].Value.ToString();
            Khac_Dich1 = dataGridViewX1.Rows[i].Cells["Khac_Dich"].Value.ToString();
            MaSP_Dich1 = dataGridViewX1.Rows[i].Cells["MaSP_Dich"].Value.ToString();
            MaCT_Dich1 = dataGridViewX1.Rows[i].Cells["MaCT_Dich"].Value.ToString();

        }
        private void save()
        {
            string insert_Nguon = "";
            Process prs = new Process();
            SqlConnection Conn = new SqlConnection();
            SqlCommand cmd = new SqlCommand();
            Conn = prs.Connect();
            
            Conn.Open();
            cmd.Connection = Conn;
            
            try
            {
                
                Nguon1 = prs.substring_right(cb_Nguon.Text, child); ;
                Dich1 = prs.substring_right(cb_Dich.Text, child);
                string[] temp = txt_Ngay_Nguon.Text.Split('/');
                Ngay_Nguon1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                NguoiGhiChep1 = txt_MaNV.Text;
                TenSP_Nguon1 = "";
                MaSP_Nguon1 = "";
                datebegin1 = mask_timebegin.Text;
                dateend1 = mask_timeend.Text;
                
                //MessageBox.Show(datebegin1.ToString());
                if (cb_TenSP_Nguon.Text != "" && cb_TenCTNguon.Text!="")
                {
                    TenSP_Nguon1 = prs.substring_left(cb_TenSP_Nguon.Text, child);
                    MaSP_Nguon1 = prs.substring_right(cb_TenSP_Nguon.Text, child);
                    MaCT_Nguon1 = prs.substring_right(cb_TenCTNguon.Text, child);
                    TenCT_Nguon1 = prs.substring_left(cb_TenCTNguon.Text, child);
                }
                else
                {
                    MaCT_Nguon1 = "";
                    TenCT_Nguon1 = "";
                }


                CDay_Nguon1 = txt_Day_Nguon.Text;
                //MessageBox.Show("check 1");
                CDay_Nguon11 = double.Parse(CDay_Nguon1);
                //MessageBox.Show("check 2");
                CRong_Nguon1 = txt_Rong_Nguon.Text;
                CRong_Nguon11 = double.Parse(CRong_Nguon1);
                CDai_Nguon1 = txt_Dai_Nguon.Text;
                CDai_Nguon11 = double.Parse(CDai_Nguon1);
                SoLuong_Nguon1 = txt_SoLuong_Nguon.Text.Trim();
               
                SoLuong_Nguon11 = double.Parse(SoLuong_Nguon1);
                NguonGo_Nguon1 = cb_NguonGo_Nguon.Text;
               
                PhanLoai_Nguon1 = cb_PhanLoai_Nguon.Text;
                NhaCungCap1 = cb_NhaCC.Text;
              // MessageBox.Show("Begin");
                insert_Nguon = @"INSERT INTO [dbo].[TKNL_Vao]
                                ([NoiGiao]
                                ,[NoiNhan]
                                ,[TenSP]
                                ,[TenCT]
                                ,[MaCT]
                                ,[CDay]
                                ,[CRong]
                                ,[CDai]
                                ,[SoLuong]
                                ,[NguonGo]
                                ,[PhanLoai]
                                ,[NguoiGhiChep]
                                ,[NhaCungCap]
                                ,[Ngay]
                                ,[GioBatDau]
                                ,[GioKetThuc])
                                VALUES(N'" + Nguon1 + "',N'" + Dich1 + "',N'" + TenSP_Nguon1 + "',N'" + TenCT_Nguon1 + "',N'" + MaCT_Nguon1 + "'," + CDay_Nguon11 + "," + CRong_Nguon11 + "," + CDai_Nguon11 + "," + SoLuong_Nguon11 + ",N'" + NguonGo_Nguon1 + "',N'" + PhanLoai_Nguon1 + "',N'" + NguoiGhiChep1 + "',N'" + NhaCungCap1 + "','" + Ngay_Nguon1 + "','"+datebegin1+"','"+dateend1+"')";
                
                cmd.CommandText = insert_Nguon;
                //MessageBox.Show(insert_Nguon);
                cmd.ExecuteNonQuery();
                maxID = @"select max(ID) from dbo.TKNL_Vao";
                
                cmd.CommandText = maxID;
                maxID = cmd.ExecuteScalar().ToString();
                for (int i = dataGridViewX1.Rows.Count - 2; i >= 0; i--)
                {
                    TenSP_Dich1 = dataGridViewX1.Rows[i].Cells["TenSP_Dich"].Value.ToString();
                    TenCT_Dich1 = dataGridViewX1.Rows[i].Cells["TenCT_Dich"].Value.ToString();
                    CDay_Dich1 = dataGridViewX1.Rows[i].Cells["ChieuDay_Dich"].Value.ToString();
                    CRong_Dich1 = dataGridViewX1.Rows[i].Cells["ChieuRong_Dich"].Value.ToString();
                    CDai_Dich1 = dataGridViewX1.Rows[i].Cells["ChieuDai_Dich"].Value.ToString();
                    SoLuong_Dich1 = dataGridViewX1.Rows[i].Cells["SoLuong"].Value.ToString();
                    SoLuong_Dich11 = double.Parse(SoLuong_Dich1);
                    NguonGo_Dich1 = dataGridViewX1.Rows[i].Cells["NguonGo_Dich"].Value.ToString();
                    PhanLoai_Dich1 = dataGridViewX1.Rows[i].Cells["PhanLoai_Dich"].Value.ToString();
                    KTG_Dich1 = dataGridViewX1.Rows[i].Cells["KTG_Dich"].Value.ToString();
                    KTG_Dich11 = double.Parse(KTG_Dich1);
                    Tramtret_Dich1 = dataGridViewX1.Rows[i].Cells["TramTret_Dich"].Value.ToString();
                    TramTret_Dich11 = double.Parse(Tramtret_Dich1);
                    TanDung_Dich1 = dataGridViewX1.Rows[i].Cells["TanDung_Dich"].Value.ToString();
                    TanDung_Dich11 = double.Parse(TanDung_Dich1);
                    Ghep_Dich1 = dataGridViewX1.Rows[i].Cells["Ghep_Dich"].Value.ToString();
                    Ghep_Dich11 = double.Parse(Ghep_Dich1);
                    MBao_Dich1 = dataGridViewX1.Rows[i].Cells["Bao_Dich"].Value.ToString();
                    MBao_Dich11 = double.Parse(MBao_Dich1);
                    MRong_Dich1 = dataGridViewX1.Rows[i].Cells["MRong_Dich"].Value.ToString();
                    MRong_Dich11 = double.Parse(MRong_Dich1);
                    MCat_Dich1 = dataGridViewX1.Rows[i].Cells["Cat_Dich"].Value.ToString();
                    MCat_Dich11 = double.Parse(MCat_Dich1);
                    Bo_Dich1 = dataGridViewX1.Rows[i].Cells["Bo_Dich"].Value.ToString();
                    Bo_Dich11 = double.Parse(Bo_Dich1);
                    Khac_Dich1 = dataGridViewX1.Rows[i].Cells["Khac_Dich"].Value.ToString();
                    Khac_Dich11 = double.Parse(Khac_Dich1);
                    MaSP_Dich1 = dataGridViewX1.Rows[i].Cells["MaSP_Dich"].Value.ToString();
                    MaCT_Dich1 = dataGridViewX1.Rows[i].Cells["MaCT_Dich"].Value.ToString();
                    insert_Nguon = @"INSERT INTO [dbo].[TKNL_Ra]
           ([ID]
           ,[TenSP]
           ,[TenCT]
           ,[MaCT]
           ,[CDay]
           ,[CRong]
           ,[CDai]
           ,[SoLuong]
           ,[SLKTG]
           ,[SLTramTret]
           ,[SLTanDung]
           ,[SLGhep]
            ,[SLBao]
            ,[SLRong]
            ,[SLCat]
           ,[SLBo]
           ,[SLKhac]
           ,[NguonGo]
           ,[PhanLoai])
            VALUES(" + maxID + ",N'" + TenSP_Dich1 + "',N'" + TenCT_Dich1 + "',N'" + MaCT_Dich1 + "'," + CDay_Dich1 + "," + CRong_Dich1 + "," + CDai_Dich1 + "," + SoLuong_Dich11 + "," + KTG_Dich11 + "," + TramTret_Dich11 + "," + TanDung_Dich11 + "," + Ghep_Dich11 + "," + MBao_Dich11 + "," + MRong_Dich11 + ","+MCat_Dich11+","+ Bo_Dich11 + "," + Khac_Dich11 + ",N'" + NguonGo_Dich1 + "',N'" + PhanLoai_Dich1 + "')";
                    cmd.CommandText = insert_Nguon;
                    
                    cmd.ExecuteNonQuery();
                    dataGridViewX1.Rows.RemoveAt(i);
                }
                MessageBox.Show("Dữ Liệu được lưu thành công");
                //prs.reset_group_txt(groupBox1);
                cb_TenSP_Nguon.Text = "";
                cb_TenCTNguon.Text = "";
                prs.reset_group_txt(groupBox2);
                txt_SoLuong_Nguon.Text = "";
                cb_TenCTDich.Text = "";
                cb_TenSP_Dich.Text = "";
                cb_TenSP_Nguon.Text = "";
                cb_TenCTNguon.Text = "";
                groupBox1.Enabled = true;
                Enable_Begin();
                cb_Nguon.Focus();
                lb_SoKhoiRa.Text = "0";
                lb_SoKhoiVao.Text = "0";

            }
            catch (Exception ex)
            {
                string delete = @"delete from dbo.TKNL_Vao where ID=" + maxID;
                cmd.CommandText = delete;
                cmd.ExecuteNonQuery();
                string s = "insert duplicate key";
                string FK = "FOREIGN KEY constraint";
                string s1 = ex.Message.ToString();
                if (s1.Contains(s))
                { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                else if (s1.Contains(FK))
                { MessageBox.Show("Nhập sai thông tin trong Combobox"); }
                else
                    MessageBox.Show(ex.Message);
                MessageBox.Show("Đã xảy ra lỗi khi thêm dữ liệu, vui lòng kiểm tra lại");
            }
            finally
            {
                cmd.Dispose();
                Conn.Close();
            }

        }

        private void dataGridViewX1_DoubleClick(object sender, EventArgs e)
        {
            Process prs = new Process();
            if (dataGridViewX1.Rows.Count > 1)
            {
                try
                {
                    int r = dataGridViewX1.CurrentCell.RowIndex;
                    //MessageBox.Show(r.ToString());
                 
                    
                    //MessageBox.Show("aaaa");
                    
                    CDay_Dich11 = double.Parse(dataGridViewX1.Rows[r].Cells["ChieuDay_Dich"].Value.ToString());
                    CRong_Dich11 = double.Parse(dataGridViewX1.Rows[r].Cells["ChieuRong_Dich"].Value.ToString());
                    CDai_Dich11 = double.Parse(dataGridViewX1.Rows[r].Cells["ChieuDai_Dich"].Value.ToString());
                    SoLuong_Dich1 = dataGridViewX1.Rows[r].Cells["SoLuong"].Value.ToString();
                    SoLuong_Dich11 = double.Parse(SoLuong_Dich1);
                    KTG_Dich1 = dataGridViewX1.Rows[r].Cells["KTG_Dich"].Value.ToString();
                    KTG_Dich11 = double.Parse(KTG_Dich1);
                    Tramtret_Dich1 = dataGridViewX1.Rows[r].Cells["TramTret_Dich"].Value.ToString();
                    TramTret_Dich11 = double.Parse(Tramtret_Dich1);
                    TanDung_Dich1 = dataGridViewX1.Rows[r].Cells["TanDung_Dich"].Value.ToString();
                    TanDung_Dich11 = double.Parse(TanDung_Dich1);
                    Ghep_Dich1 = dataGridViewX1.Rows[r].Cells["Ghep_Dich"].Value.ToString();
                    Ghep_Dich11 = double.Parse(Ghep_Dich1);
                    MBao_Dich1 = dataGridViewX1.Rows[r].Cells["Bao_Dich"].Value.ToString();
                    MBao_Dich11 = double.Parse(MBao_Dich1);
                    MRong_Dich1 = dataGridViewX1.Rows[r].Cells["MRong_Dich"].Value.ToString();
                    MRong_Dich11 = double.Parse(MRong_Dich1);
                    MCat_Dich1 = dataGridViewX1.Rows[r].Cells["Cat_Dich"].Value.ToString();
                    MCat_Dich11 = double.Parse(MCat_Dich1);
                    Bo_Dich1 = dataGridViewX1.Rows[r].Cells["Bo_Dich"].Value.ToString();
                    Bo_Dich11 = double.Parse(Bo_Dich1);
                    Khac_Dich1 = dataGridViewX1.Rows[r].Cells["Khac_Dich"].Value.ToString();
                    Khac_Dich11 = double.Parse(Khac_Dich1);
                    SoKhoi_Dich11 = CDay_Dich11 * CRong_Dich11 * CDai_Dich11 * (SoLuong_Dich11 + KTG_Dich11 + TramTret_Dich11 + TanDung_Dich11 + Ghep_Dich11 + MBao_Dich11 + MRong_Dich11 + MCat_Dich11 + Bo_Dich11 + Khac_Dich11) / 1000000000;
                    SoKhoi_Dich11 = double.Parse(lb_SoKhoiRa.Text) - SoKhoi_Dich11;
                    lb_SoKhoiRa.Text = string.Format("{0:###,###.####}", SoKhoi_Dich11);
                    
                    //int j = dataGridViewX1.Rows.Count - 1;
                    dataGridViewX1.Rows.RemoveAt(r);
                    if (dataGridViewX1.Rows.Count == 1)
                        lb_SoKhoiRa.Text = "0";
                    //dataGridViewX1.CurrentCell = dataGridViewX1.Rows[j].Cells["TenSP_Dich"];
                    prs.formatcolordatagrid(dataGridViewX1);
                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
        }

        

        private void bt_save_Click(object sender, EventArgs e)
        {
            try
            {
                save();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void FrTKNLVaoRa_KeyDown(object sender, KeyEventArgs ex)
        {
            if (ex.KeyData == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
            if (ex.KeyCode == Keys.F11)
            {
                bt_save_Click(null, null);
            }
            if (ex.KeyCode == Keys.End)
            {
                bt_insert_Click(null, null);
            }
            if (ex.KeyData == Keys.PageDown)
            {
                bt_Tinh_DMKV1_Click(null, null);
            }
        }

       

        


        private void txt_Cat_Dich_TextChanged_1(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_Cat_Dich);
        }

        private void txt_MRong_Dich_TextChanged_1(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_MRong_Dich);
        }

        private void txt_Bao_Dich_TextChanged_1(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_Bao_Dich);
        }

        private void bt_GetData_Click(object sender, EventArgs e)
        {
            try
            {
                string Data = "";
                DataSet ds = new DataSet();
                Process prs = new Process();
                string []temp = dateTimePicker1.Text.Split('/');
                //string[]  = daText.Split('/');
                Ngay_Nguon1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                string[] temp1 = dateTimePicker2.Text.Split('/');
                Ngay_Dich1 = temp1[1] + "/" + temp1[0] + "/" + temp1[2];
                #region //Data
                if (ck_SoKhoi.Checked == false)
                {
                    #region //Nguon
                    if (rd_Nguon.Checked == true)
                    {
                        if (cb_NoiGiao_S.Text == "" && cb_NoiNhan_S.Text == "")
                            Data = @"SELECT     dbo.TKNL_Vao.ID, dbo.TKNL_Vao.NoiGiao, dbo.TKNL_Vao.NoiNhan, dbo.TKNL_Vao.TenSP, dbo.TKNL_Vao.TenCT, dbo.TKNL_Vao.MaCT, 
                      dbo.TKNL_Vao.CDay, dbo.TKNL_Vao.CRong, dbo.TKNL_Vao.CDai, dbo.TKNL_Vao.SoLuong, dbo.TKNL_Vao.NguonGo, dbo.TKNL_Vao.PhanLoai, 
                      dbo.TKNL_Vao.NguoiGhiChep, dbo.TKNL_Vao.NhaCungCap, dbo.TKNL_Vao.Ngay, dbo.TKNL_Vao.Ngayinsert from TKNL_Vao where Ngay>='" + Ngay_Nguon1 + " 00:00:00' and Ngay<='" + Ngay_Dich1 + " 23:59:59'";
                        else
                        {
                            Data = @"SELECT     dbo.TKNL_Vao.ID, dbo.TKNL_Vao.NoiGiao, dbo.TKNL_Vao.NoiNhan, dbo.TKNL_Vao.TenSP, dbo.TKNL_Vao.TenCT, dbo.TKNL_Vao.MaCT, 
                      dbo.TKNL_Vao.CDay, dbo.TKNL_Vao.CRong, dbo.TKNL_Vao.CDai, dbo.TKNL_Vao.SoLuong, dbo.TKNL_Vao.NguonGo, dbo.TKNL_Vao.PhanLoai, 
                      dbo.TKNL_Vao.NguoiGhiChep, dbo.TKNL_Vao.NhaCungCap, dbo.TKNL_Vao.Ngay, dbo.TKNL_Vao.Ngayinsert from TKNL_Vao";
                            if (cb_NoiGiao_S.Text != "")
                            {
                                Nguon1 = prs.substring_right(cb_NoiGiao_S.Text, child);
                                if (cb_NoiNhan_S.Text != "")
                                {
                                    Dich1 = prs.substring_right(cb_NoiNhan_S.Text, child);
                                    Data += @" where NoiGiao=N'" + Nguon1 + "' and NoiNhan=N'" + Dich1 + "' and Ngay>='" + Ngay_Nguon1 + " 00:00:00' and Ngay<='" + Ngay_Dich1 + " 23:59:59'";
                                }
                                if (cb_NoiNhan_S.Text == "")
                                {
                                    Data += @" where NoiGiao=N'" + Nguon1 + "' and Ngay>='" + Ngay_Nguon1 + " 00:00:00' and Ngay<='" + Ngay_Dich1 + " 23:59:59'";
                                }
                            }
                            if (cb_NoiNhan_S.Text != "")
                            {
                                Dich1 = prs.substring_right(cb_NoiNhan_S.Text, child);
                                if (cb_NoiGiao_S.Text == "")
                                {
                                    Data += @" where NoiNhan=N'" + Dich1 + "' and Ngay>='" + Ngay_Nguon1 + " 00:00:00' and Ngay<='" + Ngay_Dich1 + " 23:59:59'";
                                }
                            }

                        }
                    }
                    #endregion
                    #region //Dich
                    if (rd_Dich.Checked == true)
                    {
                        if (cb_NoiGiao_S.Text == "" && cb_NoiNhan_S.Text == "")
                            Data = @"SELECT     dbo.TKNL_Ra.ID, dbo.TKNL_Ra.TenSP, dbo.TKNL_Ra.TenCT, dbo.TKNL_Ra.MaCT, dbo.TKNL_Ra.CDay, dbo.TKNL_Ra.CRong, dbo.TKNL_Ra.CDai, 
                      dbo.TKNL_Ra.SoLuong, dbo.TKNL_Ra.SLKTG, dbo.TKNL_Ra.SLTramTret, dbo.TKNL_Ra.SLTanDung, dbo.TKNL_Ra.SLGhep, dbo.TKNL_Ra.SLBao, 
                      dbo.TKNL_Ra.SLRong, dbo.TKNL_Ra.SLCat, dbo.TKNL_Ra.SLBo, dbo.TKNL_Ra.SLKhac, dbo.TKNL_Ra.NguonGo, dbo.TKNL_Ra.PhanLoai, 
                      dbo.TKNL_Ra.Serial
                        FROM         dbo.TKNL_Vao INNER JOIN
                      dbo.TKNL_Ra ON dbo.TKNL_Vao.ID = dbo.TKNL_Ra.ID where dbo.TKNL_Vao.Ngay>='" + Ngay_Nguon1 + " 00:00:00' and dbo.TKNL_Vao.Ngay<='" + Ngay_Dich1 + " 23:59:59'";
                        else
                        {
                            Data = @"SELECT     dbo.TKNL_Ra.ID, dbo.TKNL_Ra.TenSP, dbo.TKNL_Ra.TenCT, dbo.TKNL_Ra.MaCT, dbo.TKNL_Ra.CDay, dbo.TKNL_Ra.CRong, dbo.TKNL_Ra.CDai, 
                      dbo.TKNL_Ra.SoLuong, dbo.TKNL_Ra.SLKTG, dbo.TKNL_Ra.SLTramTret, dbo.TKNL_Ra.SLTanDung, dbo.TKNL_Ra.SLGhep, dbo.TKNL_Ra.SLBao, 
                      dbo.TKNL_Ra.SLRong, dbo.TKNL_Ra.SLCat, dbo.TKNL_Ra.SLBo, dbo.TKNL_Ra.SLKhac, dbo.TKNL_Ra.NguonGo, dbo.TKNL_Ra.PhanLoai, 
                      dbo.TKNL_Ra.Serial
                        FROM         dbo.TKNL_Vao INNER JOIN
                      dbo.TKNL_Ra ON dbo.TKNL_Vao.ID = dbo.TKNL_Ra.ID";
                            if (cb_NoiGiao_S.Text != "")
                            {
                                Nguon1 = prs.substring_right(cb_NoiGiao_S.Text, child);
                                if (cb_NoiNhan_S.Text != "")
                                {
                                    Dich1 = prs.substring_right(cb_NoiNhan_S.Text, child);
                                    Data += @" where dbo.TKNL_Vao.NoiGiao=N'" + Nguon1 + "' and dbo.TKNL_Vao.NoiNhan=N'" + Dich1 + "' and dbo.TKNL_Vao.Ngay>='" + Ngay_Nguon1 + " 00:00:00' and dbo.TKNL_Vao.Ngay<='" + Ngay_Dich1 + " 23:59:59'";
                                }
                                if (cb_NoiNhan_S.Text == "")
                                {
                                    Data += @" where dbo.TKNL_Vao.NoiGiao=N'" + Nguon1 + "' and dbo.TKNL_Vao.Ngay>='" + Ngay_Nguon1 + " 00:00:00' and dbo.TKNL_Vao.Ngay<='" + Ngay_Dich1 + " 23:59:59'";
                                }
                            }
                            if (cb_NoiNhan_S.Text != "")
                            {
                                Dich1 = prs.substring_right(cb_NoiNhan_S.Text, child);
                                if (cb_NoiGiao_S.Text == "")
                                {
                                    Data += @" where dbo.TKNL_Vao.NoiNhan=N'" + Dich1 + "' and dbo.TKNL_Vao.Ngay>='" + Ngay_Nguon1 + " 00:00:00' and dbo.TKNL_Vao.Ngay<='" + Ngay_Dich1 + " 23:59:59'";
                                }
                            }

                        }
                        /*Data = @"SELECT     dbo.TKNL_Ra.ID, dbo.TKNL_Ra.TenSP, dbo.TKNL_Ra.TenCT, dbo.TKNL_Ra.MaCT, dbo.TKNL_Ra.CDay, dbo.TKNL_Ra.CRong, dbo.TKNL_Ra.CDai, 
                          dbo.TKNL_Ra.SoLuong, dbo.TKNL_Ra.SLKTG, dbo.TKNL_Ra.SLTramTret, dbo.TKNL_Ra.SLTanDung, dbo.TKNL_Ra.SLGhep, dbo.TKNL_Ra.SLBao, 
                          dbo.TKNL_Ra.SLRong, dbo.TKNL_Ra.SLCat, dbo.TKNL_Ra.SLBo, dbo.TKNL_Ra.SLKhac, dbo.TKNL_Ra.NguonGo, dbo.TKNL_Ra.PhanLoai, 
                          dbo.TKNL_Ra.Serial
                            FROM         dbo.TKNL_Vao INNER JOIN
                          dbo.TKNL_Ra ON dbo.TKNL_Vao.ID = dbo.TKNL_Ra.ID
                            WHERE     (dbo.TKNL_Vao.NoiGiao = 'MRo')";*/
                    }
                    #endregion

                    ds = prs.dts(Data);
                    dataGridViewX3.DataSource = ds.Tables[0];
                    prs.formatcolordatagrid(dataGridViewX3);
                }
                #endregion
                #region //SoKhoi
                if (ck_SoKhoi.Checked == true)
                {
                    #region //Nguon
                    if (rd_Nguon.Checked == true)
                    {
                        if (cb_NoiGiao_S.Text == "" && cb_NoiNhan_S.Text == "")
                            Data = @"SELECT     NoiGiao,NoiNhan,sum(CDay*CRong*CDai*SoLuong)/1000000000 as SoKhoi
                                    FROM         dbo.TKNL_Vao
                                    where Ngay>='" + Ngay_Nguon1 + " 00:00:00' and Ngay<='" + Ngay_Dich1 + " 23:59:59' group by NoiGiao,NoiNhan";
                        else
                        {
                            Data = @"SELECT     NoiGiao,NoiNhan, sum(CDay*CRong*CDai*SoLuong)/1000000000 as SoKhoi
                                    FROM                    dbo.TKNL_Vao ";
                            if (cb_NoiGiao_S.Text != "")
                            {
                                Nguon1 = prs.substring_right(cb_NoiGiao_S.Text, child);
                                if (cb_NoiNhan_S.Text != "")
                                {
                                    Dich1 = prs.substring_right(cb_NoiNhan_S.Text, child);
                                    Data += @" where NoiGiao=N'" + Nguon1 + "' and NoiNhan=N'" + Dich1 + "' and Ngay>='" + Ngay_Nguon1 + " 00:00:00' and Ngay<='" + Ngay_Dich1 + " 23:59:59' group by NoiGiao,NoiNhan";
                                }
                                if (cb_NoiNhan_S.Text == "")
                                {
                                    Data += @" where NoiGiao=N'" + Nguon1 + "' and Ngay>='" + Ngay_Nguon1 + " 00:00:00' and Ngay<='" + Ngay_Dich1 + " 23:59:59'  group by NoiGiao,NoiNhan";
                                }
                            }
                            if (cb_NoiNhan_S.Text != "")
                            {
                                Dich1 = prs.substring_right(cb_NoiNhan_S.Text, child);
                                if (cb_NoiGiao_S.Text == "")
                                {
                                    Data += @" where NoiNhan=N'" + Dich1 + "' and Ngay>='" + Ngay_Nguon1 + " 00:00:00' and Ngay<='" + Ngay_Dich1 + " 23:59:59' group by NoiGiao,NoiNhan";
                                }
                            }

                        }
                    }
                    #endregion
                    #region //Dich
                    if (rd_Dich.Checked == true)
                    {
                        if (cb_NoiGiao_S.Text == "" && cb_NoiNhan_S.Text == "")
                            Data = @"SELECT     dbo.TKNL_Vao.NoiGiao,dbo.TKNL_Vao.NoiNhan, sum(dbo.TKNL_Ra.CDay*dbo.TKNL_Ra.CRong*dbo.TKNL_Ra.CDai*dbo.TKNL_Ra.SoLuong)/1000000000 as SoLuong, sum(dbo.TKNL_Ra.CDay*dbo.TKNL_Ra.CRong*dbo.TKNL_Ra.CDai*dbo.TKNL_Ra.SLKTG)/1000000000  as KTG, 
                      sum(dbo.TKNL_Ra.CDay*dbo.TKNL_Ra.CRong*dbo.TKNL_Ra.CDai*dbo.TKNL_Ra.SLTramTret)/1000000000  as TramTret, sum(dbo.TKNL_Ra.CDay*dbo.TKNL_Ra.CRong*dbo.TKNL_Ra.CDai*dbo.TKNL_Ra.SLTanDung)/1000000000  as TanDung, sum(dbo.TKNL_Ra.CDay*dbo.TKNL_Ra.CRong*dbo.TKNL_Ra.CDai*dbo.TKNL_Ra.SLGhep)/1000000000  as Ghep, sum(dbo.TKNL_Ra.CDay*dbo.TKNL_Ra.CRong*dbo.TKNL_Ra.CDai*dbo.TKNL_Ra.SLBao)/1000000000  as Bao, sum(dbo.TKNL_Ra.CDay*dbo.TKNL_Ra.CRong*dbo.TKNL_Ra.CDai*dbo.TKNL_Ra.SLRong)/1000000000  as Rong, sum(dbo.TKNL_Ra.CDay*dbo.TKNL_Ra.CRong*dbo.TKNL_Ra.CDai*dbo.TKNL_Ra.SLCat)/1000000000  as Cat, 
                      sum(dbo.TKNL_Ra.CDay*dbo.TKNL_Ra.CRong*dbo.TKNL_Ra.CDai*dbo.TKNL_Ra.SLBo)/1000000000  as Bo, sum(dbo.TKNL_Ra.CDay*dbo.TKNL_Ra.CRong*dbo.TKNL_Ra.CDai*dbo.TKNL_Ra.SLKhac)/1000000000  as Khac
                        FROM         dbo.TKNL_Vao INNER JOIN
                      dbo.TKNL_Ra ON dbo.TKNL_Vao.ID = dbo.TKNL_Ra.ID
                       where dbo.TKNL_Vao.Ngay>='" + Ngay_Nguon1 + " 00:00:00' and dbo.TKNL_Vao.Ngay<='" + Ngay_Dich1 + " 23:59:59' group by NoiGiao,NoiNhan";
                        else
                        {
                            Data = @"SELECT     dbo.TKNL_Vao.NoiGiao,dbo.TKNL_Vao.NoiNhan, sum(dbo.TKNL_Ra.CDay*dbo.TKNL_Ra.CRong*dbo.TKNL_Ra.CDai*dbo.TKNL_Ra.SoLuong)/1000000000 as SoLuong, sum(dbo.TKNL_Ra.CDay*dbo.TKNL_Ra.CRong*dbo.TKNL_Ra.CDai*dbo.TKNL_Ra.SLKTG)/1000000000  as KTG, 
                      sum(dbo.TKNL_Ra.CDay*dbo.TKNL_Ra.CRong*dbo.TKNL_Ra.CDai*dbo.TKNL_Ra.SLTramTret)/1000000000  as TramTret, sum(dbo.TKNL_Ra.CDay*dbo.TKNL_Ra.CRong*dbo.TKNL_Ra.CDai*dbo.TKNL_Ra.SLTanDung)/1000000000  as TanDung, sum(dbo.TKNL_Ra.CDay*dbo.TKNL_Ra.CRong*dbo.TKNL_Ra.CDai*dbo.TKNL_Ra.SLGhep)/1000000000  as Ghep, sum(dbo.TKNL_Ra.CDay*dbo.TKNL_Ra.CRong*dbo.TKNL_Ra.CDai*dbo.TKNL_Ra.SLBao)/1000000000  as Bao, sum(dbo.TKNL_Ra.CDay*dbo.TKNL_Ra.CRong*dbo.TKNL_Ra.CDai*dbo.TKNL_Ra.SLRong)/1000000000  as Rong, sum(dbo.TKNL_Ra.CDay*dbo.TKNL_Ra.CRong*dbo.TKNL_Ra.CDai*dbo.TKNL_Ra.SLCat)/1000000000  as Cat, 
                      sum(dbo.TKNL_Ra.CDay*dbo.TKNL_Ra.CRong*dbo.TKNL_Ra.CDai*dbo.TKNL_Ra.SLBo)/1000000000  as Bo, sum(dbo.TKNL_Ra.CDay*dbo.TKNL_Ra.CRong*dbo.TKNL_Ra.CDai*dbo.TKNL_Ra.SLKhac)/1000000000  as Khac
                        FROM         dbo.TKNL_Vao INNER JOIN
                      dbo.TKNL_Ra ON dbo.TKNL_Vao.ID = dbo.TKNL_Ra.ID";
                            if (cb_NoiGiao_S.Text != "")
                            {
                                Nguon1 = prs.substring_right(cb_NoiGiao_S.Text, child);
                                if (cb_NoiNhan_S.Text != "")
                                {
                                    Dich1 = prs.substring_right(cb_NoiNhan_S.Text, child);
                                    Data += @" where dbo.TKNL_Vao.NoiGiao=N'" + Nguon1 + "' and dbo.TKNL_Vao.NoiNhan=N'" + Dich1 + "' and dbo.TKNL_Vao.Ngay>='" + Ngay_Nguon1 + " 00:00:00' and dbo.TKNL_Vao.Ngay<='" + Ngay_Dich1 + " 23:59:59' group by NoiGiao,NoiNhan";
                                }
                                if (cb_NoiNhan_S.Text == "")
                                {
                                    Data += @" where dbo.TKNL_Vao.NoiGiao=N'" + Nguon1 + "' and dbo.TKNL_Vao.Ngay>='" + Ngay_Nguon1 + " 00:00:00' and dbo.TKNL_Vao.Ngay<='" + Ngay_Dich1 + " 23:59:59' group by NoiGiao,NoiNhan";
                                }
                            }
                            if (cb_NoiNhan_S.Text != "")
                            {
                                Dich1 = prs.substring_right(cb_NoiNhan_S.Text, child);
                                if (cb_NoiGiao_S.Text == "")
                                {
                                    Data += @" where dbo.TKNL_Vao.NoiNhan=N'" + Dich1 + "' and dbo.TKNL_Vao.Ngay>='" + Ngay_Nguon1 + " 00:00:00' and dbo.TKNL_Vao.Ngay<='" + Ngay_Dich1 + " 23:59:59' group by NoiGiao,NoiNhan";
                                }
                            }

                        }
                        /*Data = @"SELECT     dbo.TKNL_Ra.ID, dbo.TKNL_Ra.TenSP, dbo.TKNL_Ra.TenCT, dbo.TKNL_Ra.MaCT, dbo.TKNL_Ra.CDay, dbo.TKNL_Ra.CRong, dbo.TKNL_Ra.CDai, 
                          dbo.TKNL_Ra.SoLuong, dbo.TKNL_Ra.SLKTG, dbo.TKNL_Ra.SLTramTret, dbo.TKNL_Ra.SLTanDung, dbo.TKNL_Ra.SLGhep, dbo.TKNL_Ra.SLBao, 
                          dbo.TKNL_Ra.SLRong, dbo.TKNL_Ra.SLCat, dbo.TKNL_Ra.SLBo, dbo.TKNL_Ra.SLKhac, dbo.TKNL_Ra.NguonGo, dbo.TKNL_Ra.PhanLoai, 
                          dbo.TKNL_Ra.Serial
                            FROM         dbo.TKNL_Vao INNER JOIN
                          dbo.TKNL_Ra ON dbo.TKNL_Vao.ID = dbo.TKNL_Ra.ID
                            WHERE     (dbo.TKNL_Vao.NoiGiao = 'MRo')";*/
                    }
                    #endregion

                    ds = prs.dts(Data);
                    dataGridViewX3.DataSource = ds.Tables[0];
                    prs.formatcolordatagrid(dataGridViewX3);
                }
                #endregion

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void bt_rework_Click(object sender, EventArgs e)
        {
            Process Prs = new Process();
            Prs.reset_group_txt(groupBox1);
            Prs.reset_group_txt(groupBox2);
            groupBox1.Enabled = true;
            cb_TenSP_Nguon.Focus();
            while (dataGridViewX1.Rows.Count > 1)
            {
                dataGridViewX1.Rows.RemoveAt(0);
            }
            cb_Nguon.Enabled = true;
            cb_Dich.Enabled = true;
            lb_SoKhoiRa.Text = "0";
            lb_SoKhoiVao.Text = "0";
            bt_delete.Enabled = false;
            bt_insert.Enabled = true;
            bt_save.Enabled = true;
            lb_ID.Text = "0";

            
            
        }
        private void delete()
        {
            string delete = null;
            delete = @"delete from dbo.TKNL_Vao where ID="+lb_ID.Text;
            Data dta = new Data();
            SqlConnection Conn = dta.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;
            cmd.CommandText = delete;
            try
            {
                DialogResult d = MessageBox.Show("Bạn có chắc muốn xóa Thông Tin có Số ID " + lb_ID.Text, "Thông Báo", MessageBoxButtons.OKCancel);
                if (d == DialogResult.OK)
                {
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Dữ liệu được xóa thành công");
                    dataGridViewX1.Rows.Clear();
                    cb_Nguon.Text = "";
                    cb_Dich.Text = "";

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        private void Display()
        {
            Process prs = new Process();
            DataSet ds = new DataSet();
            try
            {
                if (rd_Nguon.Checked == true)
                {
                    int r = dataGridViewX3.CurrentCell.RowIndex;
                    string ID = dataGridViewX3.Rows[r].Cells["ID"].Value.ToString();
                    cb_Nguon.Text = dataGridViewX3.Rows[r].Cells["NoiGiao"].Value.ToString();
                    cb_Dich.Text = dataGridViewX3.Rows[r].Cells["NoiNhan"].Value.ToString();
                    cb_TenSP_Nguon.Text = dataGridViewX3.Rows[r].Cells["TenSP"].Value.ToString();
                    cb_TenCTNguon.Text = dataGridViewX3.Rows[r].Cells["TenCT"].Value.ToString();
                    txt_Day_Nguon.Text = dataGridViewX3.Rows[r].Cells["CDay"].Value.ToString();
                    txt_Rong_Nguon.Text = dataGridViewX3.Rows[r].Cells["CRong"].Value.ToString();
                    txt_Dai_Nguon.Text = dataGridViewX3.Rows[r].Cells["CDai"].Value.ToString();
                    txt_SoLuong_Nguon.Text = dataGridViewX3.Rows[r].Cells["SoLuong"].Value.ToString();
                    cb_NguonGo_Nguon.Text = dataGridViewX3.Rows[r].Cells["NguonGo"].Value.ToString();
                    cb_PhanLoai_Nguon.Text = dataGridViewX3.Rows[r].Cells["PhanLoai"].Value.ToString();
                    txt_Ngay_Nguon.Text = dataGridViewX3.Rows[r].Cells["Ngay"].Value.ToString();
                    txt_MaNV.Text = dataGridViewX3.Rows[r].Cells["NguoiGhiChep"].Value.ToString();
                    string search1 = @"SELECT     dbo.TKNL_Ra.*
                                FROM dbo.TKNL_Ra where ID='" + ID + "'";
                    ds = prs.dts(search1);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        if (dataGridViewX1.Rows.Count > 1)
                            dataGridViewX1.Rows.Clear();
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            TenSP_Dich1 = ds.Tables[0].Rows[i]["TenSP"].ToString();
                            TenCT_Dich1 = ds.Tables[0].Rows[i]["TenCT"].ToString();
                            CDay_Dich1 = ds.Tables[0].Rows[i]["CDay"].ToString();
                            CRong_Dich1 = ds.Tables[0].Rows[i]["CRong"].ToString();
                            CDai_Dich1 = ds.Tables[0].Rows[i]["CDai"].ToString();
                            SoLuong_Dich1 = ds.Tables[0].Rows[i]["SoLuong"].ToString();
                            KTG_Dich1 = ds.Tables[0].Rows[i]["SLKTG"].ToString();
                            Tramtret_Dich1 = ds.Tables[0].Rows[i]["SLTramTret"].ToString();
                            TanDung_Dich1 = ds.Tables[0].Rows[i]["SLTanDung"].ToString();
                            Ghep_Dich1 = ds.Tables[0].Rows[i]["SLGhep"].ToString();
                            MBao_Dich1 = ds.Tables[0].Rows[i]["SLBao"].ToString();
                            MRong_Dich1 = ds.Tables[0].Rows[i]["SLRong"].ToString();
                            MCat_Dich1 = ds.Tables[0].Rows[i]["SLCat"].ToString();
                            Bo_Dich1 = ds.Tables[0].Rows[i]["SLBo"].ToString();
                            Khac_Dich1 = ds.Tables[0].Rows[i]["SLKhac"].ToString();
                            NguonGo_Dich1 = ds.Tables[0].Rows[i]["NguonGo"].ToString();
                            PhanLoai_Dich1 = ds.Tables[0].Rows[i]["PhanLoai"].ToString();
                            dataGridViewX1.Rows.Add(TenSP_Dich1, TenCT_Dich1, CDay_Dich1, CRong_Dich1, CDai_Dich1, SoLuong_Dich1, NguonGo_Dich1, PhanLoai_Dich1, KTG_Dich1, Tramtret_Dich1, TanDung_Dich1, Ghep_Dich1, MBao_Dich1, MRong_Dich1, MCat_Dich1, Bo_Dich1, Khac_Dich1);

                        }
                    }
                    bt_insert.Enabled = false;
                    bt_delete.Enabled = true;
                    bt_save.Enabled = false;
                    lb_ID.Text = ID;
                    tabControl1.SelectedTab = tab_Nhap;
                }
                else
                    MessageBox.Show("Chọn option Nguồn để thực hiện chức năng này");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
            
        }

        private void dataGridViewX3_DoubleClick(object sender, EventArgs e)
        {
            Display();
        }

        private void bt_delete_Click(object sender, EventArgs e)
        {
            delete();
        }

        private void dataGridViewX4_DoubleClick(object sender, EventArgs e)
        {
            if (dataGridViewX4.Rows.Count > 1)
            {
                try
                {
                    int r = dataGridViewX4.CurrentCell.RowIndex;
                    //MessageBox.Show(r.ToString());


                    dataGridViewX4.Rows.RemoveAt(r);
                   
                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
           
            
        }

        private void txt_Day_Nguon_Leave(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_Day_Nguon);
            txt_Day_Dich.Text = txt_Day_Nguon.Text;
        }

        private void txt_Rong_Nguon_Leave(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_Rong_Nguon);
        }

        private void txt_Dai_Nguon_Leave(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_Dai_Nguon);
            txt_Dai_Dich.Text = txt_Dai_Nguon.Text;
        }

        private void txt_Day_Dich_Leave(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_Day_Dich);
        }

        private void txt_Rong_Dich_Leave(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_Rong_Dich);
        }

        private void txt_Dai_Dich_Leave(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_Dai_Dich);

        }

        private void bt_insertKB_Click(object sender, EventArgs e)
        {
            if (txt_QuyChachKB.Text != "")
            {
                dataGridViewX4.Rows.Add(txt_QuyChachKB.Text);
                txt_QuyChachKB.Text = "";
                txt_QuyChachKB.Focus();

            }
            else
            {
                MessageBox.Show("Nhập quy cách khai báo");
            }
            
        }
        private void tinh()
        {
            #region //ver 1.0
            /*try
            {
                int i = 0, QCVao,SoDu_1,thuong_1,SoDu_2,min,count,check=0;
                QCVao = int.Parse(txt_QCTinh.Text);
                count = dataGridViewX4.Rows.Count;
                int[] a = new int[count-1];
                int[] b = new int[3];
                int[] chon=new int[6];
                for (i = 0; i < count -1 ; i++)
                {
                    a[i] = int.Parse(dataGridViewX4.Rows[i].Cells[0].Value.ToString());
                   
                }
                
                if (count > 1)
                {
                    min = a[0];
                    check = 0;
                    for (i = 0; i < count - 1; i++)
                    {
                        SoDu_1 = QCVao % a[i];
                        if (SoDu_1 == 0)
                        {
                            check = 1;
                            thuong_1 = QCVao / a[i];
                            chon[0] = a[i];
                            chon[1] = thuong_1;
                            chon[2] = 0;
                            chon[3] = 0;
                            chon[4] = 0;
                            chon[5] = 0;
                            //dataGridViewX5.Rows.Add(QCVao.ToString(), a[i].ToString(), thuong_1.ToString(), "0", "0", "0", "0");
                            break;
                        }
                    }
                    //MessageBox.Show("bbbb");
                    if (check ==0)
                    {
                        chon[5]=-1;
                        for (i = 0; i < count - 1; i++)
                        {
                            SoDu_1 = QCVao % a[i];
                            thuong_1=QCVao / a[i];
                            for (int j = 0; j < count - 1; j++)
                            {
                                SoDu_2 = SoDu_1 % a[j];
                                if (min > SoDu_2)
                                {
                                    min = SoDu_2;
                                    b[0] = a[j];
                                    b[1] = SoDu_1 / a[j];
                                    b[2] = min;  
                                }
                            }
                            if (chon[5] == -1)
                            {

                                chon[0] = a[i];
                                chon[1] = thuong_1;
                                chon[2] = b[0];
                                chon[3] = b[1];
                                chon[4] = b[2];
                                chon[5] = min;
                            }
                            else
                            {
                                if (min < chon[5])
                                {
                                    chon[0] = a[i];
                                    chon[1] = thuong_1;
                                    chon[2] = b[0];
                                    chon[3] = b[1];
                                    chon[4] = b[2];
                                    chon[5] = min;
                                }
                            } 
                        }
                        //MessageBox.Show("ccc");

                    }
                    dataGridViewX5.Rows.Add(QCVao, chon[0].ToString(), chon[1].ToString(), chon[2].ToString(), chon[3].ToString(), chon[4].ToString(),chon[5].ToString());
                    txt_QCTinh.Text = (int.Parse(txt_QCTinh.Text)+100).ToString();
                }
                a.Clone();
                b.Clone();
                chon.Clone();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }*/
            #endregion
            try
            {
                int i = 0, QCVao,SoDu_1,thuong_1,SoDu_2,min,count;
                QCVao = int.Parse(txt_QCTinh.Text);
                count = dataGridViewX4.Rows.Count;
                int[] a = new int[count-1];
                int[] b = new int[3];
                int[] chon=new int[6];
                for (i = 0; i < count -1 ; i++)
                {
                    a[i] = int.Parse(dataGridViewX4.Rows[i].Cells[0].Value.ToString());
                   
                }
                
                if (count > 1)
                {
                    min = a[0];
                    chon[5]=-1;
                    for (i = 0; i < count - 1; i++)
                    {
                        SoDu_1 = QCVao % a[i];
                        thuong_1=QCVao / a[i];
                        for (int j = 0; j < count - 1; j++)
                        {
                            SoDu_2 = SoDu_1 % a[j];
                            if (SoDu_2 == 0)
                            {
                                min = SoDu_2;
                                b[0] = a[j];
                                b[1] = SoDu_1 / a[j];
                                b[2] = min;
                                break;
                            }
                            else
                            {
                                if (min > SoDu_2)
                                {
                                    min = SoDu_2;
                                    b[0] = a[j];
                                    b[1] = SoDu_1 / a[j];
                                    b[2] = min;
                                }
                            }
                        }
                        if (chon[5] == -1)
                        {

                            chon[0] = a[i];
                            chon[1] = thuong_1;
                            chon[2] = b[0];
                            chon[3] = b[1];
                            chon[4] = b[2];
                            chon[5] = min;
                        }
                        else
                        {
                            if (min < chon[5])
                            {
                                chon[0] = a[i];
                                chon[1] = thuong_1;
                                chon[2] = b[0];
                                chon[3] = b[1];
                                chon[4] = b[2];
                                chon[5] = min;
                            }
                        }
                        if (min == 0)
                            break;
                    }
                    dataGridViewX5.Rows.Add(QCVao, chon[0].ToString(), chon[1].ToString(), chon[2].ToString(), chon[3].ToString(), chon[4].ToString(),chon[5].ToString());
                    txt_QCTinh.Text = (int.Parse(txt_QCTinh.Text)+100).ToString();
                }
                a.Clone();
                b.Clone();
                chon.Clone();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bt_tinh_Click(object sender, EventArgs e)
        {
            tinh();
            
            txt_QCTinh.Focus();
            int j = dataGridViewX5.Rows.Count - 1;
            dataGridViewX5.CurrentCell = dataGridViewX5.Rows[j].Cells[0];
            dataGridViewX5.FirstDisplayedCell = dataGridViewX5.Rows[j].Cells[0];

        }

       

        private void dataGridViewX5_DoubleClick(object sender, EventArgs e)
        {
            if (dataGridViewX5.Rows.Count > 1)
            {
                try
                {
                    int r = dataGridViewX5.CurrentCell.RowIndex;
                    //MessageBox.Show(r.ToString());


                    dataGridViewX5.Rows.RemoveAt(r);

                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
        }

        private void txt_QuyChachKB_Leave(object sender, EventArgs e)
        {
            int check = 0;
            try
            {
                check = int.Parse(txt_QuyChachKB.Text);
            }
            catch
            {
                MessageBox.Show("Giá trị nhập phải là số");
                txt_QuyChachKB.Text = "";
            }
        }

        private void txt_QCTinh_Leave(object sender, EventArgs e)
        {
            int check = 0;
            try
            {
                check = int.Parse(txt_QCTinh.Text);
            }
            catch
            {
                MessageBox.Show("Giá trị nhập phải là số");
                txt_QCTinh.Text = "";
            }
        }

        private void bt_Tinh_DMKV1_Click(object sender, EventArgs e)
        {
            try
            {
                
                if (dataGridViewX1.Rows.Count > 1)
                {
                    SoLuong_Nguon11 = 0;
                    for (int i = 0; i < dataGridViewX1.Rows.Count - 1; i++)
                    {
                        SoLuong_Nguon11 += double.Parse(dataGridViewX1.Rows[i].Cells["KTG_Dich"].Value.ToString()) + double.Parse(dataGridViewX1.Rows[i].Cells["TramTret_Dich"].Value.ToString()) + double.Parse(dataGridViewX1.Rows[i].Cells["TanDung_Dich"].Value.ToString()) + double.Parse(dataGridViewX1.Rows[i].Cells["Ghep_Dich"].Value.ToString()) + double.Parse(dataGridViewX1.Rows[i].Cells["Bao_Dich"].Value.ToString()) + double.Parse(dataGridViewX1.Rows[i].Cells["MRong_Dich"].Value.ToString()) + double.Parse(dataGridViewX1.Rows[i].Cells["Cat_Dich"].Value.ToString()) + double.Parse(dataGridViewX1.Rows[i].Cells["Bo_Dich"].Value.ToString()) + double.Parse(dataGridViewX1.Rows[i].Cells["Khac_Dich"].Value.ToString());
                    }
                    txt_SoLuong_Nguon.Text = SoLuong_Nguon11.ToString();
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

      

     


        

      
    }
}
